//src/main/java/com/cuit/blog/controller/loginController.java
package com.cuit.blog.controller;
import com.cuit.blog.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/login")
public class loginController {
    @Autowired
    private UserService userService;
    @GetMapping("")
    public String login(Model model){
        return "login";
    }
    @PostMapping("")
    public String authenticate(@RequestParam String username, @RequestParam String password, HttpServletRequest request, Model model) {
        if (userService.authenticate(username, password)) {
            request.getSession().setAttribute("username", username);
            return "redirect:/index";
        } else {
            return "login";
        }
    }

}